package com.miaowang.salad.activity;

import prj.carmap.mobile.CarmapApplication;
import prj.carmap.mobile.R;
import prj.carmap.mobile.activity.MainActivity;
import prj.carmap.mobile.process.AutoServiceProcess;
import prj.carmap.mobile.process.LiveServiceProcess;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.actionbarsherlock.app.ActionBar.LayoutParams;
import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.Menu;
import com.miaowang.salad.data.OkHttpResponseData;
import com.miaowang.salad.jobqueue.JobEvents;
import com.miaowang.salad.jobqueue.LogoutJob;
import com.miaowang.salad.util.PrefUtils;
import com.path.android.jobqueue.Job;

import de.greenrobot.event.EventBus;
import de.greenrobot.event.Subscribe;
import de.greenrobot.event.ThreadMode;

public class MineInfoActivity extends SherlockActivity {

	TextView tvMyService;
	TextView tvMyCar;
	private ProgressDialog pd;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		CarmapApplication.currentPage = "mine";
		CarmapApplication.addActivity(this);
		setContentView(R.layout.fragment_mine);

		// Inflate the custom view
		View customNav = LayoutInflater.from(this).inflate(
				R.layout.nav_seller_title_view, null);
		// Attach to the action bar
		getSupportActionBar().setDisplayShowCustomEnabled(true);
		getSupportActionBar().setDisplayShowHomeEnabled(false);
		getSupportActionBar().setDisplayShowTitleEnabled(false);
		LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT,
				LayoutParams.WRAP_CONTENT, Gravity.CENTER);
		getSupportActionBar().setCustomView(customNav);
		customNav.setLayoutParams(lp);
		Button returnBtn = (Button) customNav.findViewById(R.id.return_prev);
		Button btnLogout = (Button) customNav.findViewById(R.id.return_map);
		btnLogout.setVisibility(View.VISIBLE);
		btnLogout.setText("退出登录");
		returnBtn.setOnClickListener(new Button.OnClickListener() {// 创建监听
					public void onClick(View v) {
						// ProfileActivity.this.startActivity(new Intent(
						// ProfileActivity.this, MainActivity.class));
						MineInfoActivity.this.startActivity(new Intent(
								MineInfoActivity.this, MainActivity.class));
						((Activity) MineInfoActivity.this).finish();
					}
				});
		btnLogout.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (pd == null) {
					pd = new ProgressDialog(MineInfoActivity.this);
				}
				pd.setTitle("正在注销...");
				pd.show();
				Job job = new LogoutJob(MineInfoActivity.this, "", "");
				CarmapApplication.getInstance().getJobManager()
						.addJobInBackground(job);
			}
		});
		((TextView) customNav.findViewById(R.id.nav_title)).setText("我的");
		tvMyService = (TextView) findViewById(R.id.tv_my_service);
		tvMyCar = (TextView) findViewById(R.id.tv_my_car);
		initMenuItem();
		initControl();
	}

	@Override
	protected void onResume() {
		CarmapApplication.currentPage = "mine";
		super.onResume();
	}

	private void initControl() {
		tvMyService.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent();
				intent.setClass(MineInfoActivity.this, MyServiceActivity.class);
				MineInfoActivity.this.startActivity(intent);
			}
		});

		tvMyCar.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent();
				intent.setClass(MineInfoActivity.this, UserInfoActivity.class);
				MineInfoActivity.this.startActivity(intent);
			}
		});
	}

	@SuppressWarnings("deprecation")
	private void initMenuItem() {
		if ("type".equals(CarmapApplication.currentPage)) {
			// 底部菜单项
			View menuItem = this.findViewById(R.id.menu_item_type);
			menuItem.setBackgroundDrawable(getApplication().getResources()
					.getDrawable(R.drawable.bottom_background));
			// 底部菜单图标
			ImageView itemImage = (ImageView) this
					.findViewById(R.id.item_type_image);
			itemImage.setImageResource(R.drawable.type_select);
			// 底部菜单文字
			TextView itemText = (TextView) this
					.findViewById(R.id.item_type_text);
			itemText.setTextColor(getApplication().getResources().getColor(
					R.color.white));
		}
		// else if ("search".equals(CarmapApplication.currentPage)) {
		// // 底部菜单项
		// View menuItem = this.findViewById(R.id.menu_item_search);
		// menuItem.setBackgroundDrawable(getApplication().getResources().getDrawable(R.drawable.bottom_background));
		// // 底部菜单图标
		// ImageView itemImage = (ImageView)
		// this.findViewById(R.id.item_search_image);
		// itemImage.setImageResource(R.drawable.search_select);
		// // 底部菜单文字
		// TextView itemText = (TextView)
		// this.findViewById(R.id.item_search_text);
		// itemText.setTextColor(getApplication().getResources().getColor(R.color.white));
		// }
		else if ("mine".equals(CarmapApplication.currentPage)) {
			// 底部菜单项
			View menuItem = this.findViewById(R.id.menu_item_mine);
			menuItem.setBackgroundDrawable(getApplication().getResources()
					.getDrawable(R.drawable.bottom_background));
			// 底部菜单图标
			ImageView itemImage = (ImageView) this
					.findViewById(R.id.item_mine_image);
			itemImage.setImageResource(R.drawable.mine_select);
			// 底部菜单文字
			TextView itemText = (TextView) this
					.findViewById(R.id.item_mine_text);
			itemText.setTextColor(getApplication().getResources().getColor(
					R.color.white));
		} else if ("salad".equals(CarmapApplication.currentPage)) {
			// 底部菜单项
			View menuItem = this.findViewById(R.id.menu_item_salad);
			menuItem.setBackgroundDrawable(getApplication().getResources()
					.getDrawable(R.drawable.bottom_background));
			// 底部菜单图标
			ImageView itemImage = (ImageView) this
					.findViewById(R.id.item_salad_image);
			itemImage.setImageResource(R.drawable.mine_select);
			// 底部菜单文字
			TextView itemText = (TextView) this
					.findViewById(R.id.item_salad_text);
			itemText.setTextColor(getApplication().getResources().getColor(
					R.color.white));
		}
		// else if ("more".equals(CarmapApplication.currentPage)) {
		// // 底部菜单项
		// View menuItem = this.findViewById(R.id.menu_item_more);
		// menuItem.setBackgroundDrawable(getApplication().getResources().getDrawable(R.drawable.menu_color_base));
		// // 底部菜单图标
		// ImageView itemImage = (ImageView)
		// this.findViewById(R.id.item_more_image);
		// itemImage.setImageResource(R.drawable.more_select);
		// // 底部菜单文字
		// TextView itemText = (TextView)
		// this.findViewById(R.id.item_more_text);
		// itemText.setTextColor(getApplication().getResources().getColor(R.color.white));
		// }
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		EventBus.getDefault().register(this);
		View view = (View) this.findViewById(R.id.menu_item_home);
		view.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				AutoServiceProcess process = new AutoServiceProcess();
				process.setContext(MineInfoActivity.this);
				process.getSellerTypes();
				MineInfoActivity.this.finish();
			}
		});

		view = (View) this.findViewById(R.id.menu_item_type);
		view.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				LiveServiceProcess process = new LiveServiceProcess();
				process.setContext(MineInfoActivity.this);
				process.getSellerTypes();
				MineInfoActivity.this.finish();
			}
		});

		View saladView = (View) this.findViewById(R.id.menu_item_salad);
		saladView.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(MineInfoActivity.this,
						SaladMainActivity.class);
				// intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				MineInfoActivity.this.startActivity(intent);
				MineInfoActivity.this.finish();
			}

		});

		// view = (View) this.findViewById(R.id.menu_item_more);
		// view.setOnClickListener(new OnClickListener() {
		// @Override
		// public void onClick(View v) {
		// ProfileActivity.this.startActivity(new Intent(ProfileActivity.this,
		// MoreActivity.class));
		// }
		// });

		// view = (View) this.findViewById(R.id.menu_item_search);
		// view.setOnClickListener(new OnClickListener() {
		// @Override
		// public void onClick(View v) {
		// CarmapApplication.popView = new DialogView(ProfileActivity.this);
		// CarmapApplication.popView.show();
		// ProfileActivity.this.startActivity(new Intent(ProfileActivity.this,
		// SellerSearchActivity.class));
		// ProfileActivity.this.finish();
		// }
		// });

		return super.onCreateOptionsMenu(menu);
	}
	
	@Subscribe(threadMode = ThreadMode.MainThread)
	public void onEventMainThread(JobEvents.JobSuccess e) {
		if(e.getJobCode() == JobEvents.LOGOUT){
			if (pd != null)
				pd.dismiss();
			OkHttpResponseData msg = (OkHttpResponseData) e.getResult();
			int code = msg.getCode();
			if (code == 0) {
				PrefUtils.clear(MineInfoActivity.this);
				Intent intent = new Intent(MineInfoActivity.this,UserLoginActivity.class);
				MineInfoActivity.this.startActivity(intent);
				MineInfoActivity.this.finish();
			} else {
				Toast.makeText(MineInfoActivity.this, msg.getMsg(),
						Toast.LENGTH_SHORT).show();
			}
		}
	}

	@Subscribe(threadMode = ThreadMode.MainThread)
	public void onEventMainThread(JobEvents.JobFailure e) {
		if(e.getJobCode() == JobEvents.LOGOUT){

			if (pd != null)
				pd.dismiss();
			Toast.makeText(MineInfoActivity.this, "网络请求失败",
					Toast.LENGTH_SHORT).show();

		}
	}
	
	@Override
	protected void onDestroy() {
		EventBus.getDefault().unregister(this);
		super.onDestroy();
	}
}
